import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import axios from 'axios';

const getList=async()=>[
     await axios.get('http://localhost:7001/shop/getshopList').then(res=>{
        console.log(res);
     })
]
getList()

export interface IndexModelState {
  name: string;
}

export interface IndexModelType {
  namespace: 'index';
  state: IndexModelState;
  effects: {
    query: Effect;
  };
  reducers: {
    save: Reducer<IndexModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions: { setup: Subscription };
}

const IndexModel: IndexModelType = {
  namespace: 'index',

  state: {
    name: 'umijs 您好',
  },

  effects: {
    *query({ payload }, { call, put }) {
      console.log('effects',payload);
      // call 异步函数
      // put 调用同步方法
      yield put({
        type:"save",
        payload:'567'
      })
    },
  },
  reducers: {
    save(state, action) {
      console.log('reducer',state,action);
      
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  //页面初始化进入的时候只有connect 连接上之后，此属性就会执行
  subscriptions: {
    setup({ dispatch, history }) {
      console.log('subscriptions');
      dispatch({
        type:'query',
        payload:'234'
      })
    },
  },
};

export default IndexModel;

